package app.r_teenus.frontend.controller;

import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import app.r_teenus.backend.model.ServiceOrder;


@Component
public class ServiceOrderValidator {

	/**
	 * Spring Web Flow activated validation (validate + ${state}). Validates 'serviceOrderForm' view state after binding.
	 */
	public void validateServiceOrderForm(ServiceOrder serviceOrder, MessageContext context) {
		if (serviceOrder.getServiceAction() != null) {
			if (serviceOrder.getServiceAction().getAmount() == null || serviceOrder.getServiceAction().getAmount() < 0){
					context.addMessage(new MessageBuilder().error().source("amount").code("serviceOrder.form.amount.error").build());
			}
			
			if (serviceOrder.getServiceAction().getPrice() == null || serviceOrder.getServiceAction().getPrice() < 0){
					context.addMessage(new MessageBuilder().error().source("price").code("serviceOrder.form.price.error").build());
			}
			
			if (!StringUtils.hasText(serviceOrder.getServiceAction().getDescription())) {
				context.addMessage(new MessageBuilder().error().source("work").code("serviceOrder.form.work.error").build());
			}
			
			
			if (!StringUtils.hasText(serviceOrder.getServicePart().getName())) {
				context.addMessage(new MessageBuilder().error().source("name").code("servicePart.form.name.error").build());
			}
			if (serviceOrder.getServicePart().getCount() == null || serviceOrder.getServicePart().getCount() < 0)
				context.addMessage(new MessageBuilder().error().source("count").code("servicePart.form.count.error").build());

			if (serviceOrder.getServiceAction().getPrice() == null || serviceOrder.getServiceAction().getPrice() < 0)
				context.addMessage(new MessageBuilder().error().source("price").code("servicePart.form.price.error").build());
		}
	}

}
